//
//  ZGLNewsDetViewController.m
//  Faker
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLNewsDetViewController.h"
#import "ZGLSubDetModel.h"
#import "ZGLNaviView.h"

@interface ZGLNewsDetViewController ()

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) ZGLNaviView *titleView;

@end

@implementation ZGLNewsDetViewController

- (void)dealloc {
    [_webView.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}



- (void)setNavigationBar {
    _titleView = [[ZGLNaviView alloc] initWithFrame:CGRectMake(0, 0, ScaleX * 375, 64)];
    [_titleView.backButton addTarget:self action:@selector(backButton:) forControlEvents:(UIControlEventTouchUpInside)];
    _titleView.shareButton.hidden = YES;
    [self.view addSubview:_titleView];
}


- (void)backButton:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    self.view.backgroundColor = backColor;
    _titleView.backgroundColor = naviColor;
}
- (void)createWebView {
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, ScaleX * 375, ScaleY * 647)];
    
    [_webView.scrollView addObserver:self forKeyPath:@"contentOffset" options:1 | 2 context:nil];
    
    [self.view addSubview:_webView];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGPoint point = [change[@"new"] CGPointValue];
    if (point.y < 0) {
        _webView.scrollView.scrollEnabled = NO;
    } else {
        _webView.scrollView.scrollEnabled = YES;
    }
}
- (void)setSubModel:(ZGLSubDetModel *)subModel {
    if (_subModel != subModel) {
        _subModel = subModel;
    }
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:subModel.weburl]]];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self createWebView];
    [self setNavigationBar];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
